package com.chj.comp.common.core.util;

import cn.hutool.core.text.StrFormatter;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;

/**
 * @author ：cuihui
 * @date ：Created in 2024/10/10
 * @description：高德地图工具类
 * @modified By：
 */
public class GaoDeUtil {
    private static final String KEY = "a2682b6b8614c4f983a1ea90942fc92b";

    /**
     * 根据地址、城市获取经纬度
     * @param address 结构化地址信息
     * @param city 指定查询的城市
     * @return
     */
    public static String getLocationByAddress(String address, String city){
        String url = "https://restapi.amap.com/v3/geocode/geo?key=" + KEY + "&address={}&city={}";
        String response = HttpUtil.sendGet(StrFormatter.format(url, address, city));
        JSONObject jsonObject = JSONObject.parseObject(response);
        Object status = jsonObject.get("status");
        if ("1".equals(status)){
            JSONArray geocodes = JSONArray.parseArray(jsonObject.get("geocodes").toString());
            return JSONObject.parseObject(geocodes.get(0).toString()).get("location").toString();
        }
        return null;
    }

    /**
     * 根据坐标获取区划编码
     * @param location 经纬度
     * @return
     */
    public static String getAreaCodeByLocation(String location){
        String url = "https://restapi.amap.com/v3/geocode/regeo?key=" + KEY + "&location={}";
        String response = HttpUtil.sendGet(StrFormatter.format(url, location));
        JSONObject jsonObject = JSONObject.parseObject(response);
        Object status = jsonObject.get("status");
        if ("1".equals(status)){
            return JSONObject.parseObject(JSONObject.parseObject(jsonObject.get("regeocode").toString()).get("addressComponent").toString()).get("towncode").toString();
        }
        return null;
    }

    /**
     * 根据IP获取区划编码
     * @param ip
     * @return
     */
    public static String getAreaCodeByIP(String ip){
        String url = "https://restapi.amap.com/v3/ip?key=" + KEY + "&ip={}";
        String response = HttpUtil.sendGet(StrFormatter.format(url, ip));
        JSONObject jsonObject = JSONObject.parseObject(response);
        Object status = jsonObject.get("status");
        if ("1".equals(status)){
            return jsonObject.get("adcode").toString();
        }
        return null;
    }

    /**
     *
     * @param location 坐标点:经度和纬度用","分割，经度在前，纬度在后，经纬度小数点后不得超过6位
     * @param coordsys 可选值：gps;mapbar;baidu;autonavi(不进行转换)
     * @return
     */
    public static String convertLocation(String location, String coordsys){
        String url = "https://restapi.amap.com/v3/assistant/coordinate/convert?key=" + KEY + "&locations={}&coordsys={}";
        String response = HttpUtil.sendGet(StrFormatter.format(url, location, coordsys));
        JSONObject jsonObject = JSONObject.parseObject(response);
        Object status = jsonObject.get("status");
        if ("1".equals(status)){
            return jsonObject.get("locations").toString();
        }
        return null;
    }

    public static Map<String, String> getWeatherByAreaCode(String areaCode){
        String url = "https://restapi.amap.com/v3/weather/weatherInfo?key=" + KEY + "&city={}";
        String response = HttpUtil.sendGet(StrFormatter.format(url, areaCode));
        JSONObject jsonObject = JSONObject.parseObject(response);
        Object status = jsonObject.get("status");
        if ("1".equals(status)){
            return (Map<String, String>) JSONArray.parseArray(jsonObject.get("lives").toString()).get(0);
        }
        return null;
    }

    public static void main(String[] args) {
//        System.out.println(GaoDeUtil.getLocationByAddress("青海湖景区售票中心", "青海省"));
//        System.out.println(GaoDeUtil.getAreaCodeByLocation("100.228654,36.894097"));
//        System.out.println(GaoDeUtil.getAreaCodeByIP("61.178.231.34"));
//        System.out.println(GaoDeUtil.convertLocation("103.691617,36.500878", "baidu"));
        System.out.println(GaoDeUtil.getWeatherByAreaCode("620121"));
    }
}
